package controller;

import com.github.pagehelper.PageInfo;
import entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import service.CustomerService;
import service.ServiceService;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

/**
 * @author lay
 * @date 2020/6/22 10:54
 */
@Controller
@RequestMapping("/service")
public class ServiceController {
    @Autowired
    private ServiceService serviceService;
    @Autowired
    private CustomerService customerService;

    /**
     * layui添加功能
     * @return
     */
    @RequestMapping("/toAdd")
    public String toAdd(HttpSession httpSession){
        List<Customer> customers = customerService.selectAll();
        httpSession.setAttribute("customers",customers);
        return "add.jsp";
    }
    @ResponseBody
    @RequestMapping("/add")
    public MyResult add(HService hService){
        System.out.println(hService);
        serviceService.insert(hService);
        return new MyResult<>(0);
    }
    /**
     * layui修改功能
     * @param id
     * @param modelMap
     * @return
     */

    @RequestMapping("/toUpdate")
    public String toUpdate(Integer id, ModelMap modelMap,HttpSession httpSession){
        HService hService = serviceService.selectById(id);
        modelMap.addAttribute("hService",hService);
        List<Customer> customers = customerService.selectAll();
        httpSession.setAttribute("customers",customers);
        return "update.jsp";
    }
    @ResponseBody
    @RequestMapping("/update")
    public MyResult update(HService hService){
        serviceService.update(hService);
        return new MyResult<>(0);
    }
    /**
     * layui批量删除功能
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping("/batchDelete")
    public MyResult batchDelete(Integer[] ids){
        System.out.println(Arrays.toString(ids));
        serviceService.batchDelete(ids);
        return new MyResult<>(0);
    }
    /**
     * layui查询功能
     * @return
     */
    @RequestMapping("/toSearch")
    public String toSearch(HttpSession httpSession){
        List<Customer> customers = customerService.selectAll();
        httpSession.setAttribute("customers",customers);
        System.out.println("customers"+customers.toString());
        return "search.jsp";
    }
    @ResponseBody
    @RequestMapping("/search")
    public MyResult<List<HService>> search( LayRequest layRequest){
        //执行分页语句
        PageInfo<HService> servicePageInfo = serviceService.selectByPage(layRequest);
        //将分页查询结果传递到页面
        return new MyResult<>(0,servicePageInfo.getTotal(),servicePageInfo.getList());
    }
    /**
     * layui转让功能
     * @return
     */
    @RequestMapping("/toTransfer")
    public String toTransfer(){
        return "transfer.jsp";
    }
    @ResponseBody
    @RequestMapping("/transfer")
    public MyResult<List<HService>> transfer( LayRequest layRequest){

        return new MyResult<>(0);
    }

    /**
     * layui列表页面
     * @param layRequest
     * @return
     */
    @ResponseBody
    @RequestMapping("/list")
    public MyResult<List<HService>> list(LayRequest layRequest, HttpSession httpSession){
        List<Customer> customers = customerService.selectAll();
        httpSession.setAttribute("customers",customers);
        //执行分页语句
        PageInfo<HService> servicePageInfo = serviceService.selectByPage(layRequest);
        //将分页查询结果传递到页面
        return new MyResult<>(0,servicePageInfo.getTotal(),servicePageInfo.getList());
    }

}
